package com.py.framework.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.service.ActDefinedService;

/**
 * 流程定义管理Controller
 * @author Leegern
 * @date   2018年5月22日
 */
@RestController
@RequestMapping("/act")
public class ActDefinedController extends SystemController {
	
	@Autowired
	private ActDefinedService actDefinedService;
	
	/**
	 * 
	 * @api {post} /act/queryActDefinedDataList.json 分页查询工作流程定义列表
	 * @apiName queryActDefinedDataList
	 * @apiVersion 0.0.1
	 * @apiGroup 流程定义管理
	 * @apiParam {int} page  当前页码
	 * @apiParam {int} limit 每页条数
	 * @apiParam {String} actKey 流程键
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} processName 流程名称 
	 * @apiSuccess {String} processKey 键
	 * @apiSuccess {String} processVersion 版本号
	 * @apiSuccess {String} deploymentId 序号
	 * @apiSuccess {String} deploymentName 模型名称
	 * @apiSuccess {String} deploymentTime 发布时间
	 * @apiSuccessExample {json} 成功返回结果
	 *  {"code":0,"msg":null,"count":2,"data":[{"processName":"流程1","processKey":"pro_key","processVersion":"v_1.0","deploymentId":10,"deploymentName":"请假流程","deploymentTime":"2018-10-01"}]}
	 */
	@RequestMapping(value = "/queryActDefinedDataList", method = RequestMethod.POST)
	public RestServiceResult<List<Map<String, Object>>> queryActDefinedDataList(int page, int limit, String actKey) {
		Page pager = super.getPage(page, limit);
		List<Map<String, Object>> datas = actDefinedService.queryActDefinedDataList(pager, actKey);
		return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
	}
	
	/**
	 * @api {post} /act/delActDefined.json 删除流程
	 * @apiName delActDefined
	 * @apiVersion 0.0.1
	 * @apiGroup  流程定义管理
	 * @apiParam {String} actKey
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} result
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":2,"data":null}
	 */
	@RequestMapping(value = "/delActDefined", method = RequestMethod.POST)
	public RestServiceResult<Object> delActDefined(String actKey) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (StringUtils.isNotBlank(actKey)) {
			try {
				actDefinedService.delActDefined(actKey);
			} catch (ServiceException e) {
				result = super.getFailedRestServiceResult(e.getMessage());
				ExceptionLogger.error(e);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("删除失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
}